/*
 * FindReplacePanel
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of FindReplacePanel.
 *
 * FindReplacePanel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FindReplacePanel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console.findreplace;

import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
import javax.swing.*;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
class OptionsComponent extends JPanel {

    public static Map<String, Action> makeActions(Preferences preferences) {
        final ResourceBundle bundle = ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle");

        final Action matchCaseAction = new ToggleAction(
            "OptionsComponent.matchCaseCheckBox",
            bundle, FindReplacePanel.CMD_MATCH_CASE, preferences,
            FindReplacePanel.DEFAULT_MATCH_CASE_SELECTED
        );

        final Action wholeWordsAction = new ToggleAction(
            "OptionsComponent.wholeWordsCheckBox",
            bundle, FindReplacePanel.CMD_WHOLE_WORDS, preferences,
            FindReplacePanel.DEFAULT_WHOLE_WORDS_SELECTED
        );

        final Action regularExpressionAction = new ToggleAction(
            "OptionsComponent.regexCheckBox",
            bundle, FindReplacePanel.CMD_REGULAR_EXPRESSION, preferences,
            FindReplacePanel.DEFAULT_REGULAR_EXPRESSION_SELECTED
        ) {
            @Override public void putValue(String key, Object value) {
                super.putValue(key, value);

                if (Action.SELECTED_KEY.equals(key)) {
                    if (Actions.isSelected(this)) {
                        wholeWordsAction.setEnabled(false);
                    }
                    else {
                        wholeWordsAction.setEnabled(true);
                    }
                }
            }
        };

        
        Map<String, Action> actions = new HashMap<String, Action>(5);
        actions.put(FindReplacePanel.CMD_MATCH_CASE, matchCaseAction);
        actions.put(FindReplacePanel.CMD_WHOLE_WORDS, wholeWordsAction);
        actions.put(FindReplacePanel.CMD_REGULAR_EXPRESSION, regularExpressionAction);
        
        return actions;
    }


    /** Creates new form OptionsComponent */
    public OptionsComponent(Map<String, Action> actions) {
        initComponents();

        matchCaseCheckBox.setAction((Action) actions.get(FindReplacePanel.CMD_MATCH_CASE));
        wholeWordsCheckBox.setAction((Action) actions.get(FindReplacePanel.CMD_WHOLE_WORDS));
        regexCheckBox.setAction((Action) actions.get(FindReplacePanel.CMD_REGULAR_EXPRESSION));
        highlightCheckBox.setAction((Action) actions.get(FindReplacePanel.CMD_HIGHLIGHT));
        
        hideButton.setAction(actions.get(FindReplacePanel.CMD_HIDE));
    }


    public JButton getHideButton() {
      return hideButton;
    }

    public JCheckBox getHighlightCheckBox() {
      return highlightCheckBox;
    }

    public JCheckBox getMatchCaseCheckBox() {
      return matchCaseCheckBox;
    }

    public JCheckBox getRegexCheckBox() {
      return regexCheckBox;
    }

    public JCheckBox getWholeWordsCheckBox() {
      return wholeWordsCheckBox;
    }

    public void addActionListener(ActionListener a) {
        hideButton.addActionListener(a);
        matchCaseCheckBox.addActionListener(a);
        wholeWordsCheckBox.addActionListener(a);
        regexCheckBox.addActionListener(a);
        highlightCheckBox.addActionListener(a);
    }

    public void removeActionListener(ActionListener a) {
        hideButton.removeActionListener(a);
        matchCaseCheckBox.removeActionListener(a);
        wholeWordsCheckBox.removeActionListener(a);
        regexCheckBox.removeActionListener(a);
        highlightCheckBox.removeActionListener(a);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        matchCaseCheckBox = new javax.swing.JCheckBox();
        wholeWordsCheckBox = new javax.swing.JCheckBox();
        regexCheckBox = new javax.swing.JCheckBox();
        highlightCheckBox = new javax.swing.JCheckBox();
        hideButton = new javax.swing.JButton();

        matchCaseCheckBox.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("OptionsComponent.matchCaseCheckBox.mnemonic").charAt(0));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle"); // NOI18N
        matchCaseCheckBox.setText(bundle.getString("OptionsComponent.matchCaseCheckBox.text")); // NOI18N

        wholeWordsCheckBox.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("OptionsComponent.wholeWordsCheckBox.mnemonic").charAt(0));
        wholeWordsCheckBox.setText(bundle.getString("OptionsComponent.wholeWordsCheckBox.text")); // NOI18N

        regexCheckBox.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("OptionsComponent.regexCheckBox.mnemonic").charAt(0));
        regexCheckBox.setText(bundle.getString("OptionsComponent.regexCheckBox.text")); // NOI18N

        highlightCheckBox.setMnemonic(java.util.ResourceBundle.getBundle("simplex3d/console/findreplace/Bundle").getString("OptionsComponent.highlightCheckBox.mnemonic").charAt(0));
        highlightCheckBox.setText(bundle.getString("OptionsComponent.highlightCheckBox.text")); // NOI18N

        hideButton.setText(bundle.getString("OptionsComponent.hideButton.text")); // NOI18N
        hideButton.setToolTipText(bundle.getString("OptionsComponent.hideButton.tooltip")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(matchCaseCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(wholeWordsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(regexCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addComponent(highlightCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hideButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(matchCaseCheckBox)
                .addComponent(wholeWordsCheckBox)
                .addComponent(regexCheckBox)
                .addComponent(hideButton)
                .addComponent(highlightCheckBox))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton hideButton;
    private javax.swing.JCheckBox highlightCheckBox;
    private javax.swing.JCheckBox matchCaseCheckBox;
    private javax.swing.JCheckBox regexCheckBox;
    private javax.swing.JCheckBox wholeWordsCheckBox;
    // End of variables declaration//GEN-END:variables

}
